package com.idbk.chargestation.api;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.RetryPolicy;
import com.android.volley.toolbox.StringRequest;
import com.idbk.chargestation.AppContext;
import com.idbk.chargestation.net.EHttpResponse;

import java.util.HashMap;
import java.util.Map;

/**
 * 充电点相关的API
 *
 * @author zhx
 */
public class APIForCharge {

    /**
     * 发送请求并在请求中 添加token信息
     *
     * @param URL      请求路径
     * @param params   请求参数
     * @param response 请求回调
     */
    private static StringRequest postWithToken(
            String URL,
            final Map<String, String> params,
            final EHttpResponse response) {
        AppContext appContext = AppContext.getInstance();
        String token = appContext.getToken();
        if (token != null && token.length() > 1) {
            params.put("token", appContext.getToken());
        }
        return EHttpWrapper.post(URL, params, response);
    }


    /**
     * 枪状态刷新
     *
     * @param pileSn 充电桩序列号
     */
    public static StringRequest getChargeGunStatus(String pileSn, EHttpResponse response) {
        final String URL = ChargeStationURL.format("/charge/startcharge!status.do");
        Map<String, String> params = new HashMap<String, String>();
        String gunNum = "00";
        params.put("model.pileSn", pileSn);
        params.put("model.gunNum",  gunNum);
        return APIForCharge.postWithToken(URL, params, response);
    }

    /**
     * 开始充电
     *
     * @param pileSn       充电桩序列号
     * @param chargeMethod 充电方式
     * @param chargeLimit  充电方式输入(金额、时间或电量)
     * @param gunNum       枪口选择
     * @param voltageBMS   BMS电压参数
     */
    public static StringRequest startCharge(
            String pileSn,
            String chargeMethod,
            String chargeLimit,
            String gunNum,
            int voltageBMS,
            final EHttpResponse response) {
        final String URL = ChargeStationURL.format("/charge/startcharge.do");
        final Map<String, String> params = new HashMap<String, String>();
        params.put("model.pileSn", pileSn);
        params.put("model.chargeMethod", chargeMethod);
        if (chargeLimit != null) {
            params.put("model.chargeLimit", chargeLimit);
        }
        params.put("model.gunNum", gunNum);
        params.put("model.voltage_BMS", voltageBMS + "");
        AppContext appContext = AppContext.getInstance();
        String token = appContext.getToken();
        if (token != null && token.length() > 1) {
            params.put("token", appContext.getToken());
        }
        StringRequest request = new StringRequest(
                com.android.volley.Request.Method.POST,
                URL,
                response.getmListener(),
                response.getmErrorListener()) {

            @Override
            protected void onFinish() {
                super.onFinish();

                response.onEFinish();
            }

            @Override
            protected Map<String, String> getParams()
                    throws AuthFailureError {
                return params;
            }

        };

        int socketTimeout = 300000;//30 seconds - change to what you want
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 0f);
        request.setRetryPolicy(policy);


        AppContext.getInstance().getVolleyQueue().add(request);

        return request;
    }


    /**
     * 充电刷新
     *
     * @param pileSn 充电桩序列号，为空的时候查询用户当前充电
     */
    public static StringRequest refreshCharge(String pileSn, final EHttpResponse response) {
        final String URL = ChargeStationURL.format("/charge/startcharge!refresh.do");
        final Map<String, String> params = new HashMap<String, String>();
        //pileSn
        if (pileSn != null) {
            params.put("model.pileSn", pileSn);
        }
        AppContext appContext = AppContext.getInstance();
        String token = appContext.getToken();
        if (token != null && token.length() > 1) {
            params.put("token", appContext.getToken());
        }

        StringRequest request = new StringRequest(
                com.android.volley.Request.Method.POST,
                URL,
                response.getmListener(),
                response.getmErrorListener()) {

            @Override
            protected void onFinish() {
                super.onFinish();

                response.onEFinish();
            }

            @Override
            protected Map<String, String> getParams()
                    throws AuthFailureError {
                return params;
            }

        };

        int socketTimeout = 30000;//30 seconds - change to what you want
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 0f);
        request.setRetryPolicy(policy);


        AppContext.getInstance().getVolleyQueue().add(request);

        return request;
    }


    /**
     * 充电刷新
     * @param pileSn 充电桩序列号，为空的时候查询用户当前充电
     */
    public static StringRequest refreshCharge(String pileSn,  String gunNum, final EHttpResponse response){
        final String URL = ChargeStationURL.format("/charge/startcharge!refresh.do");
        final Map<String, String> params = new HashMap<String, String>();
        //pileSn
        if(pileSn != null){
            params.put("model.pileSn",pileSn);
        }
        params.put("model.gunNum",gunNum);
        AppContext appContext = AppContext.getInstance();
        String token = appContext.getToken();
        if(token != null && token.length() > 1){
            params.put("token", appContext.getToken());
        }

        StringRequest request = new StringRequest(
                com.android.volley.Request.Method.POST,
                URL,
                response.getmListener(),
                response.getmErrorListener()){

            @Override
            protected void onFinish() {
                super.onFinish();

                response.onEFinish();
            }

            @Override
            protected Map<String, String> getParams()
                    throws AuthFailureError {
                return params;
            }

        };

        int socketTimeout = 30000;//30 seconds - change to what you want
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        request.setRetryPolicy(policy);


        AppContext.getInstance().getVolleyQueue().add(request);

        return request;
    }

    /**
     * 企业号获取充电列表
     */
    public static StringRequest getChargeList(final EHttpResponse response){
        final String URL = ChargeStationURL.format("/charge/chargedlist.do");
        final Map<String, String> params = new HashMap<String, String>();
        AppContext appContext = AppContext.getInstance();
        String token = appContext.getToken();
        if(token != null && token.length() > 1){
            params.put("token", appContext.getToken());
        }

        StringRequest request = new StringRequest(
                com.android.volley.Request.Method.POST,
                URL,
                response.getmListener(),
                response.getmErrorListener()){

            @Override
            protected void onFinish() {
                super.onFinish();

                response.onEFinish();
            }

            @Override
            protected Map<String, String> getParams()
                    throws AuthFailureError {
                return params;
            }

        };

        int socketTimeout = 30000;
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        request.setRetryPolicy(policy);


        AppContext.getInstance().getVolleyQueue().add(request);

        return request;
    }

    /**
     * 停止充电
     *
     * @param pileSn 充电桩序列号
     */
    public static StringRequest stopCharge(String pileSn, String gunNum, final EHttpResponse response) {

        final String URL = ChargeStationURL.format("/charge/stopcharge.do");
        final Map<String, String> params = new HashMap<String, String>();
        params.put("model.pileSn", pileSn);
        params.put("model.gunNum", gunNum);

        AppContext appContext = AppContext.getInstance();
        String token = appContext.getToken();
        if (token != null && token.length() > 1) {
            params.put("token", appContext.getToken());
        }

        StringRequest request = new StringRequest(
                com.android.volley.Request.Method.POST,
                URL,
                response.getmListener(),
                response.getmErrorListener()) {

            @Override
            protected void onFinish() {
                super.onFinish();

                response.onEFinish();
            }

            @Override
            protected Map<String, String> getParams()
                    throws AuthFailureError {
                return params;
            }

        };

        int socketTimeout = 300000;//30 seconds - change to what you want
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 0f);
        request.setRetryPolicy(policy);


        AppContext.getInstance().getVolleyQueue().add(request);

        return request;
    }

    /**
     * 查询当前 自己的充电相关的信息：是否在预约，是否在充电等
     */
    public static StringRequest getMyChargeInfo(EHttpResponse response) {
        final String URL = ChargeStationURL.format("/charge/chargetab.do");
        Map<String, String> params = new HashMap<String, String>();
        return postWithToken(URL, params, response);
    }

    /**
     * 充电桩扫码
     *
     * @param pileSn   电桩序列号
     * @param response 回调
     */
    public static StringRequest qrScan(String pileSn, final EHttpResponse response) {
        final String url = ChargeStationURL.format("/charge/connect.do");
        final Map<String, String> params = new HashMap<String, String>();
        params.put("model.pileSn", pileSn);
        AppContext appContext = AppContext.getInstance();
        String token = appContext.getToken();
        if (token != null && token.length() > 1) {
            params.put("token", appContext.getToken());
        }

        StringRequest request = new StringRequest(
                com.android.volley.Request.Method.POST,
                url,
                response.getmListener(),
                response.getmErrorListener()) {

            @Override
            protected void onFinish() {
                super.onFinish();
                response.onEFinish();
            }

            @Override
            protected Map<String, String> getParams()
                    throws AuthFailureError {
                return params;
            }

        };

        int socketTimeout = 300000;//30 seconds - change to what you want
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 0f);
        request.setRetryPolicy(policy);


        AppContext.getInstance().getVolleyQueue().add(request);

        return request;
    }

    /**
     * 地锁下方请求
     *
     * @param pileSn   电桩序列号
     * @param lockNo   地锁号
     * @param response 回调
     */
    public static StringRequest lockRequest(String pileSn, String lockNo, EHttpResponse response) {
        final String URL = ChargeStationURL.format("/charge/groundlock.do");
        Map<String, String> params = new HashMap<String, String>();
        params.put("model.pileSn", pileSn);
        params.put("model.lockNo", lockNo);
        return postWithToken(URL, params, response);
    }


}
